﻿using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.UI.Game.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using System;
using Microsoft.Xna.Framework.Input;

namespace Epitech.Krisk.Xna.UI.Game.Menu.Intro
{
    class Intro : GameScreen
    {
        RenderBackground _bg;
        ISequenceIntro _sequence;
        Conquete _conquete;
        Strategie _strategie;
        Interactivite _interactivite;

        ContentManager _content;

        public override void Draw(GameTime gameTime)
        {
            _bg.Draw(gameTime);
            _sequence.Draw(gameTime, ScreenManager.SpriteBatch);
        }

        public override void LoadContent()
        {
            _content = new ContentManager(ScreenManager.Game.Services, "Content");
            _bg = new RenderBackground("Intro/Background");
            _bg.Content = ScreenManager.Game.Content;
            _bg.SpriteBatch = ScreenManager.SpriteBatch;
            _bg.ViewPort = ScreenManager.GraphicsDevice.Viewport;
            _bg.LoadContent();

            _conquete = new Conquete();
            _conquete.LoadContent(_content);
            _sequence = _conquete;

            _strategie = new Strategie();
            _strategie.LoadContent(_content);

            _interactivite = new Interactivite();
            _interactivite.LoadContent(_content);
        }

        public override void UnloadContent()
        {
            _sequence.UnloadContent();
        }

        TimeSpan _time = TimeSpan.Zero;

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (_time.TotalSeconds > 7.0)
                gotoMenu();
            else if (_time.TotalSeconds > 5.0)
                _sequence = _interactivite;
            else if (_time.TotalSeconds > 3.0)
                _sequence = _strategie;
            _bg.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            _sequence.Update(gameTime, _time);
            _time += gameTime.ElapsedGameTime;
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        #region Handle Input

        public override void HandleInput(InputState input)
        {
            PlayerIndex tmp;

            if (input.GetPressedKeys().Length > 0)
                gotoMenu();
            if (input.IsMouseButtonDown(MouseButton.Left))
                gotoMenu();
            if (input.IsNewButtonPress(Buttons.Start, null, out tmp)
                || input.IsNewButtonPress(Buttons.A, null, out tmp))
                gotoMenu();
        }

        protected void gotoMenu()
        {
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new MainMenu(), null);
        }

        #endregion
    }
}

